<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\NewsCategory */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="news-category-form">

    <?php $form = ActiveForm::begin([
        'id' => 'clientform'
    ]); ?>

    <?= $form->field($model, 'parent_id')->hiddenInput()->label(false) ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'icon')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'order')->textInput(['type' => 'number']) ?>

    <?= $form->field($model, 'status')->dropDownList(\common\helpers\TStringHelper::getStatusText()) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>

    var operation = "";

    var formId = "clientform";

    $("#"+formId).on("submit", function(event) {
        event.preventDefault();
        var validateResult = $("#"+formId).data('yiiActiveForm').validated;
        if (validateResult == true) {
            submitModalForm(formId,"formModal",true,true);
        }

    });

    function FormSubmit()
    {
        $("#"+formId).submit();
    }


    function submitModalForm(formId, modalId,  isClose, isErrorSubmit) {
        var $form = $('#'+formId), THIS = this;
        var actionUrl = $form.attr("action");
        $.post(
            actionUrl,
            $form.serialize()
        )
            .done(function(data) {
                var result = data.result;
                if (result === 'other'){
                    alert(data.message);
                }
                else if (result === 'failure') {
                    if (isErrorSubmit)
                    {
                        $form.submit();
                    }
                    else {
                        alert('操作失败.');
                    }
                }
                else
                {
                    ReloadPageAfterUpdate(modalId, isClose, actionUrl);
                    reloadtree();
                }
                'function' == typeof THIS.callback && THIS.callback();
            })
            .fail(function() {
                alert("内部错误");
            });
        return false;
    }
</script>